﻿#region Header
// Copyright 2007-2011, Jerónimo Milea
// 
// This file is part of NetJsWire.
// 
//     NetJsWire is free software: you can redistribute it and/or modify
//     it under the terms of the GNU General Public License as published by
//     the Free Software Foundation, either version 3 of the License, or
//     (at your option) any later version.
// 
//     NetJsWire is distributed in the hope that it will be useful,
//     but WITHOUT ANY WARRANTY; without even the implied warranty of
//     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//     GNU General Public License for more details.
// 
//     You should have received a copy of the GNU General Public License
//     along with NetJsWire.  If not, see <http://www.gnu.org/licenses/>.
#endregion

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Globalization;
using System.Windows.Forms;
using NetJsWire.Lite.Widgets.UI.Data;

namespace NetJsWire.Lite.Converters
{
    public class StoreTypeConverter : ReferenceConverter
    {
        // Fields
        private readonly ReferenceConverter listConverter;

        // Methods
        public StoreTypeConverter()
            : base( typeof( IStore ) )
        {
            listConverter = new ReferenceConverter( typeof( IList ) );
        }

        public override object ConvertTo( ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType )
        {
            if ( ( destinationType == typeof( string ) ) && ( value is Type ) )
            {
                return value.ToString();
            }
            //if ( destinationType == typeof( string ) )
            //{
            //    return value.ToString();
            //}

            return base.ConvertTo( context, culture, value, destinationType );
        }

        //public override object ConvertFrom( ITypeDescriptorContext context, CultureInfo culture, object value )
        //{
        //    if ( value is string )
        //        return value.ToString();

        //    return base.ConvertFrom( context, culture, value );
        //}

        public override StandardValuesCollection GetStandardValues( ITypeDescriptorContext context )
        {
            var list = new ArrayList( base.GetStandardValues( context ) );
            var standardValues = listConverter.GetStandardValues( context );
            var values = new ArrayList();
            var instance = context.Instance as BindingSource;
            foreach ( var obj2 in list )
            {
                if ( obj2 != null )
                {
                    var attribute = ( ListBindableAttribute ) TypeDescriptor.GetAttributes( obj2 )[ typeof( ListBindableAttribute ) ];
                    if ( ( ( attribute == null ) || attribute.ListBindable ) && ( ( instance == null ) || ( instance != obj2 ) ) )
                    {
                        var table = obj2 as DataTable;
                        if ( ( table == null ) || !list.Contains( table.DataSet ) )
                        {
                            values.Add( obj2 );
                        }
                    }
                }
            }
            foreach ( var obj3 in standardValues )
            {
                if ( obj3 != null )
                {
                    var attribute2 = ( ListBindableAttribute ) TypeDescriptor.GetAttributes( obj3 )[ typeof( ListBindableAttribute ) ];
                    if ( ( ( attribute2 == null ) || attribute2.ListBindable ) && ( ( instance == null ) || ( instance != obj3 ) ) )
                    {
                        values.Add( obj3 );
                    }
                }
            }
            values.Add( null );
            return new StandardValuesCollection( values );
        }

        public override bool GetStandardValuesExclusive( ITypeDescriptorContext context )
        {
            return false;
        }

        public override bool GetStandardValuesSupported( ITypeDescriptorContext context )
        {
            return true;
        }
    }


}
